package com.cnlive.logpare.util

import scala.collection.mutable.HashMap
import java.net.URL
import java.net.URLDecoder

object ParseLogUtil {
  def main(args: Array[String]): Unit = {
    val data=new HashMap[String, String]()
    val dd="""/page.js?sver=3.1&uid=bxxc943627_1498452542000&tm=1498452550000&from=cnlive&spid=cnlive&appid=web_cnlive&version=1.0.0&ref=http%3A%2F%2Fimage.baidu.com%2Fsearch%2Fdetail%3Ftn%3Dbaiduimagedetail%26word%3D%25E5%25B9%25BC%25E5%25A5%25B3%26ie%3Dutf-8%26step_word%3D%26ipn%3Dd%26istype%3D0%26os%3D2447535970%2C3548495788%26cs%3D4180881299%2C35230035%26di%3D0%26pn%3D2%26simi%3Dcardserver%26simics%3D1136522600%252C1460068582%26cardserver%3D1%26srctype%3D14%26bdtype%3D14%26ctd%3D1498452460145%5E3_1349X619%251&uri=http%3A%2F%2Fsports.china.com.cn%2Ftxt%2F2007-12%2F06%2Fcontent_9351325.htm"""
    val dsfds="""/page.js?sver=3.1&uid=5qp9ex3309_1494258679000&tm=1494258679000&from=cnlive&spid=cnlive&appid=web_cnlive&version=1.0.0&ref=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DwNNXxqeGzjT5i0hF_KcKcYBsTJL1cZcT6s4nNa32v2MXCQHsMQfHQVReFc9Qc0GRcSmGcejim4q1gfx9WI93jNe2n7L9P5Cg2Sf8iY_UTEazzATWdgKFnBJxe29JNMX6%26wd%3D%26eqid%3Dc8c2d04100003aac000000065910921d%26tn%3D22073068_3_oem_dg&uri=http%3A%2F%2Fsports.china.com.cn%2Fquanminjianshen%2Fdetail1_2015_11%2F02%2F470843.html"""
    val ndpahe="""http://sports.china.com.cn/zuqiu/detail1_2016_02/17/483051.html web/"""
    val url="""http://gateway.gaohuitong.com/gateway/quickpay/redirect?payResult=/gateway/success&returnUrl=http%3A%2F%2F120.92.18.252%3A8080%2Fpay%2Fquick%2Fnotify%3FtranTime%3D20170731000000%26platform%3D%26tradeStatus%3D02%26tranAmt%3D0.1%26bindId%3Dnull%26tranNo%3D20170731140410147668%26payAcctNo%3D0773%26bankCode%3DABC%26sign%3Delmad7ysX3s3VdDAUNrZJs%2BTQOyR%2B6EfB5Rl32LpJlnvXCnSogYtCspn3tAomML2DetPIC93e4c1Xqjf54xIjWpOm8baDagG0K7Morj%2B4JTI%2FqVc8Nw6kLd%2B66tjc09dWJ4Ti4KUAAMrCd1AiWGSsrPTZsRhCpNSR8e%2Bw5STb0GNCw51D0BwIx9IMMWn5SWJ77xJ7X24IuEqBhtWdbnkiiwOE1MQDZaXA%2Fl%2BTfL4FNuGa43LgMWfwBLy65B%2FQ1ZSGy1iwc4CshGjEP0tDnJGbY3UUfPryYE1Mfb5QoiMpNM677MOE0B%2Ba2%2FVz6zoCZM0A4HvYySbE1fsAxCxk7Wndw%3D%3D%26retCode%3D0000%26retMsg%3D%E4%BA%A4%E6%98%93%E6%88%90%E5%8A%9F%26merchantId%3D000110048160140%26merOrderId%3D20170731140332%26merAcctId%3DpayerId&msg=%25E4%25BA%25A4%25E6%2598%2593%25E6%2588%2590%25E5%258A%259F"""
    val usrl="""msg=%E4%BA%A4%E6%98%93%E6%88%90%E5%8A%9F""";
    println( URLDecoder.decode(usrl.replaceAll("%(?![0-9a-fA-F]{2})", "%25").replaceAll("\\+", "%2B"),"utf-8"));

    //parseUrlToMap(dd,data)
     //println(data)
    // println(playParseMap(ndpahe,data))
     //println(data)
  }
  val url=new URL("""http://www.baidu.com""")
  def parseIptoLocation(iPSeekerExt:IPSeekerExt,ip:String,dataMap:HashMap[String, String]){
        val d=iPSeekerExt.analyticIp(ip)
        val city=d.getCity
        val province=d.getProvince
        val country=d.getCountry
        val operator=d.getOperator
        dataMap.put("ip", ip);//城市
        dataMap.put("location", province);//省份
        dataMap.put("city", city);//城市
        dataMap.put("operator", operator+"")//运营商
  }
  //解析
  def platParseToMap(text:String,mapRequest:HashMap[String, String]){
    mapRequest.put("plat",get_mobile_system(text.toLowerCase())+"")
  }
  def parseUrl(url:String,cout:Int):String={
    if(url.indexOf("%") != -1){
      val tempurl= URLDecoder.decode(url.replaceAll("%(?![0-9a-fA-F]{2})", "%25").replaceAll("\\+", "%2B"),"utf-8")
      if(cout<5){
        parseUrl(tempurl,cout+1)
      }else{
        tempurl
      }
    }else{
      val tempurl= URLDecoder.decode(url.replaceAll("\\+", "%2B"),"utf-8")
      tempurl.replaceAll("http:////", "http://")
    }
  }
  def parseParams(strUrlParam:String,mapRequest:HashMap[String, String]){
    val strUrlParams=strUrlParam.split("&")
       for(strParam<-strUrlParams){
            val strParams=strParam.split("=")
            if(strParams.length==2){
              mapRequest.put(strParams(0).toLowerCase(),parseUrl(strParams(1),0))
            }else{
              mapRequest.put(strParams(0).toLowerCase(),"")
           }
       }
  }
  
  def playParseMap(text:String,mapRequest:HashMap[String, String]):String={
     if(text.indexOf("android-app://") != -1){
       val temUrl=new URL(text.replaceFirst("android-app", "http"))
       val strUrlParam=temUrl.getQuery
       if(strUrlParam!=null){
         parseParams(strUrlParam,mapRequest)
       }
       temUrl.getHost
     }else if(text.indexOf("about://") != -1){
       val temUrl=new URL(text.replaceFirst("about", "http"))
       val strUrlParam=temUrl.getQuery
       if(strUrlParam!=null){
         parseParams(strUrlParam,mapRequest)
       }
       temUrl.getHost
     }else {
       val temUrl=new URL(text)
       val strUrlParam=temUrl.getQuery
       if(strUrlParam!=null){
         parseParams(strUrlParam,mapRequest)
       }
       temUrl.getHost
     }
  }
  /**
   * scala解析url信息
   * 
   */
  def parseUrlToMap(parseUrlText:String,dataMap:HashMap[String, String]){
    val tempUrl=new URL(url,parseUrlText)
    val strUrlParam=tempUrl.getQuery
    if(strUrlParam!=null){
      parseParams(strUrlParam,dataMap)
    }else{
       dataMap.put("nocanshu", "")
    }
   
  }
  /**
   * 获得domain
   */
  def get_domain(uri:String):String={
    if(uri.indexOf("app.shineup.com.cn") != -1)"app.shineup.com.cn" //全匹配
    else if(uri.indexOf("cnlive.com") != -1)"cnlive.com"
    //else if(uri.indexOf("www.cnlive.com") != -1)"cnlive.com"
    else if(uri.indexOf("life.cnlive.com") != -1)"life.cnlive.com"  //elife.cnlive.com 还有个
    else if(uri.indexOf("wepiao.com") != -1)"wepiao.com"
    else if(uri.indexOf("shoukan.cnlive.com") != -1)"shoukan.cnlive.com"  //全匹配
    else if(uri.indexOf("h5.svipmovie.com") != -1)"h5.svipmovie.com"  //全匹配
    else if(uri.indexOf("cxxs.cnlive.com") != -1)"cxxs.cnlive.com"    //全匹配
    else if(uri.indexOf("shineup.com.cn") != -1)"shineup.com.cn"   //app.shineup.com.cn  api2017.shineup.com.cn
    else if(uri.indexOf("me.cnlive.com") != -1)"me.cnlive.com"
    else if(uri.indexOf("cxxs.china.com.cn") != -1)"cxxs.china.com.cn" //全匹配
    else if(uri.indexOf("heihei.mobi") != -1)"heihei.mobi"
    else if(uri.indexOf("theonebeijing.com") != -1)"theonebeijing.com"  //www.theonebeijing.com
    else if(uri.indexOf("h5.betgame.ibbt.tv") != -1)"h5.betgame.ibbt.tv" //全匹配
    else if(uri.indexOf("sports.china.com.cn") != -1)"sports.china.com.cn" //全匹配
    else if(uri.indexOf("sports.china.cnlive.com") != -1)"sports.china.cnlive.com" //全匹配
    else if(uri.indexOf("scio.gov.cn") != -1)"scio.gov.cn"   //s.scio.gov.cn
    else if(uri.indexOf("m.svipmovie.com") != -1)"m.svipmovie.com"
    else if(uri.indexOf("www.ff-8.com") != -1)"www.ff-8.com"   //全匹配
    else if(uri.indexOf("www.cn-healthcare.com") != -1)"www.cn-healthcare.com"
    else if(uri.indexOf("fc.cnlive.com") != -1)"fc.cnlive.com"  //bm.fc.cnlive.com 
    else if(uri.indexOf("weixue.cnlive.com") != -1)"weixue.cnlive.com"
    else if(uri.indexOf("vc.cnlive.com") != -1)"vc.cnlive.com"  //adminvc.cnlive.com mvc.cnlive.com
    else if(uri.indexOf("tingjiancn.com") != -1)"tingjiancn.com"
    else if(uri.indexOf("lehua.cnlive.com") != -1)"lehua.cnlive.com"//全匹配
    else if(uri.indexOf("liuliang.cnlive.com") != -1)"liuliang.cnlive.com"
    else if(uri.indexOf("changyule.cnlive.com") != -1)"changyule.cnlive.com"//全匹配
    else if(uri.indexOf("youyang.yz188.cn") != -1)"youyang.yz188.cn"
    else if(uri.indexOf("me.cnlive.com") != -1)"me.cnlive.com"
    else if(uri.indexOf("ffyl88.cnlive.com") != -1)"ffyl88.cnlive.com"//全匹配
    else if(uri.indexOf("h5.hrtv.cn") != -1)"h5.hrtv.cn"
    else if(uri.indexOf("yixian365.cnlive.com") != -1)"yixian365.cnlive.com"
    else if(uri.indexOf("www.51ctovc.com") != -1)"www.51ctovc.com"
    else if(uri.indexOf("ffyl.cnlive.com") != -1)"ffyl.cnlive.com"//全匹配
    else "null"
  }
  /**
   * 返回手机类型--机型
   */
  def get_decive(browser:String):Int={
    if(browser.indexOf("iphone") != -1)101//"iphone"
    else if(browser.indexOf("ipad") != -1)102//"ipad"
    else if(browser.indexOf("samsung") != -1 || browser.indexOf("sm-") != -1|| browser.indexOf("sch-") != -1|| browser.indexOf("gt-") != -1|| browser.indexOf(" shv-") != -1)103//"三星"
    else if(browser.indexOf("redmi") != -1)104//"红米"
    else if(browser.indexOf("xiaomi") != -1|| browser.indexOf("hm") != -1|| browser.indexOf("mi ") != -1|| browser.indexOf("mi-") != -1)105//"小米"
    else if(browser.indexOf("sony") != -1)106//"索尼"
    else if(browser.indexOf("lenovo") != -1 ||browser.indexOf("zuk") != -1)107//"联想"
    else if(browser.indexOf("huawei") != -1||browser.indexOf("hw-") != -1||browser.indexOf("che1-") != -1)108//"华为"
    else if(browser.indexOf("honor") != -1)109//"荣耀"
    else if(browser.indexOf("vivo") != -1)110//"vivo"
    else if(browser.indexOf("oppo") != -1)111//"oppo"
    else if(browser.indexOf("hisense") != -1)112//"海信"
    else if(browser.indexOf("coolpad") != -1)113//"酷派"
    else if(browser.indexOf("nexus") != -1)114//"nexus"
    else if(browser.indexOf("meizu") != -1||browser.indexOf(" mx") != -1||browser.indexOf("mz-") != -1||browser.indexOf("m3 note") != -1)115//"魅族"
    else if(browser.indexOf("daxian") != -1)116//"大显"
    else if(browser.indexOf("zte") != -1)117//"中兴"
    else if(browser.indexOf("htc") != -1)118//"htc"
    else if(browser.indexOf("le ") != -1||browser.indexOf("letv") != -1||browser.indexOf(" le") != -1)119//"乐视"
    else if(browser.indexOf("aoledior ") != -1)120//"aoledior"
    else if(browser.indexOf("koobee") != -1)121//"酷比"
    else if(browser.indexOf("yepen") != -1)122//"誉品"
    else if(browser.indexOf("readboy") != -1)123//"读书郎"
    else if(browser.indexOf("nokia") != -1)124//"诺基亚"
    else if(browser.indexOf("meetuu") != -1)125//"美图"
    else if(browser.indexOf(" lg") != -1)126//"lg"
    else if(browser.indexOf("m1") != -1||browser.indexOf(" sm") != -1)127//"锤子"
    else if(browser.indexOf("hl-") != -1)128//"海尔"
    else if(browser.indexOf("ivvi") != -1)129//"ivvi"
    else 199//"其它移动端"
  }
  /**
   * 返回手机浏览器类型
   */
  def get_mobile_browser(browser:String):Int={
    if(browser.indexOf("mqqbrowser") != -1)101//"手机QQ浏览器"
    else if(browser.indexOf("sogoumobilebrowser") != -1)102//"搜狗手机浏览器"
    else if(browser.indexOf("360browser") != -1)103//"360手机浏览器"
    else if(browser.indexOf("mb2345browser") != -1)104//"2345手机浏览器"
    else if(browser.indexOf("baidubrowser") != -1)105//"百度手机浏览器"
    else if(browser.indexOf("wkbrowser") != -1)106//"微信内置浏览器"
    else if(browser.indexOf("ucweb") != -1 || browser.indexOf("ucbrowser") != -1)107//"手机UC浏览器"
    else if(browser.indexOf("baiduboxapp") != -1)108//"百度盒子app"
    else if(browser.indexOf("opera") != -1)109//"opera手机浏览器"
    else if(browser.indexOf("miuibrowser") != -1)110//"小米内置浏览器"
    else if(browser.indexOf("nokiabrowser") != -1)111//"诺基亚内置浏览器"
    else if(browser.indexOf("samsungbrowser") != -1 || browser.indexOf("samsungbrowser") != -1)112//"三星手机浏览器"
    else if(browser.indexOf("vivobrowser") != -1)113//"vivo内置浏览器"
    else if(browser.indexOf("oppobrowser") != -1)114//"oppo内置浏览器"
     //以上的条件判断浏览器相对准确,以下条件相对不准,故要分清判断的先后顺序
    else if(browser.indexOf("iphone os") != -1 && browser.indexOf("khtml, like gecko") != -1)115//"iPhone内置浏览器"
    else if(browser.indexOf("mac os") != -1 && browser.indexOf("khtml, like gecko") != -1)116//"iPad内置浏览器"
    else if(browser.indexOf("khtml, like gecko") != -1 && browser.indexOf("chrome") != -1)117//"手机chrome浏览器"
    else if(browser.indexOf("android") != -1 && browser.indexOf("safari") != -1)118//"安卓浏览器"
    else 199//"其它移动端浏览器"
  }
  /**
   * 获取PC浏览器类型
   */
  def get_pc_browser(browser:String):Int={
    if(browser.indexOf("360se") != -1)201//"360安全浏览器"
    else if(browser.indexOf("360ee") != -1)202//"360极速浏览器"
    else if((browser.indexOf("firefox") != -1)
        ||((browser.indexOf("(x") != -1)&&(browser.indexOf("linux") != -1)&&(browser.indexOf("rv") != -1))
       ||(browser.indexOf("(windows; u;") != -1)&&(browser.indexOf("windows nt") != -1)
       &&(browser.indexOf("zh") != -1)
       &&(browser.indexOf("rv") != -1)
    )203//"火狐浏览器"
    else if(browser.indexOf("metasr") != -1)204//"搜狗高速浏览器"
    else if(browser.indexOf("theworld") != -1)205//"世界之窗浏览器"
    else if(browser.indexOf("maxthon") != -1)206//"遨游浏览器"
    else if(browser.indexOf("lbbrowser") != -1)7//"猎豹浏览器"
    else if(browser.indexOf("2345") != -1)208//"2345浏览器"
    else if(browser.indexOf("qqbrowser") != -1 || browser.indexOf("tencenttraveler") != -1 )209//"QQ浏览器"
    else if(browser.indexOf("yandex") != -1)210//"yandex浏览器"
    else if(browser.indexOf("yunguance") != -1)211//"百度云观测爬虫"
    else if((browser.indexOf("chrome") != -1)&&(browser.indexOf("applewebkit") != -1)&&(browser.indexOf("like gecko") != -1)&&(browser.indexOf("khtml") != -1))212//"谷歌浏览器"
    else if(((browser.indexOf("compatible") != -1)&&(browser.indexOf("msie") != -1))
        ||(browser.indexOf("rv:11") != -1))213//"IE浏览器"
    else if(((browser.indexOf("safari") != -1)&&(browser.indexOf("applewebkit") != -1)
        &&(browser.indexOf("like gecko") != -1)&&(browser.indexOf("khtml") != -1))||
        (browser.indexOf("macintosh; intel mac os x") != -1)
        )214//"safari浏览器"
    else if(browser.indexOf("bing") != -1)215//"必应爬虫"
    else if(browser.indexOf("baiduspider") != -1)216//"百度爬虫"
    else if(browser.indexOf("googlebot") != -1)217//"谷歌爬虫"
    else if(browser.indexOf("msnbot-media") != -1)218//"微软爬虫"
    else 299//"其它PC浏览器"
  } 
  /**
   * 获取手机系统类型
   */
   def get_mobile_system(browser:String):Int={
     if(browser.indexOf("android") != -1)101//"android"
     else if(browser.indexOf("windows") != -1)102//"windowsPhone"
     else if(browser.indexOf("mac os x") != -1||browser.indexOf("ios") != -1)103//"ios"
     else if(browser.indexOf("meego") != -1)105//"MeeGo"
     else if(browser.indexOf("j2me") != -1||browser.indexOf("midp") != -1)106//"JavaME"
     else if(browser.indexOf("linux") != -1)104//"手机linux系统"
     else 199//其他移动端系统
   }
  /**
   * 获取电脑的系统
   */
  def get_pc_system(browser:String):Int={
    if(browser.indexOf("windows 95") != -1)201//"Windows 95"
    else if(browser.indexOf("windows 98") != -1)202//"windows 98"
    else if(browser.indexOf("windows nt 5.0") != -1)203//"Windows 2000"
    else if(browser.indexOf("windows nt 6.0") != -1)204//"Windows vista"
    else if(browser.indexOf("windows nt 5.1") != -1 || browser.indexOf("windows nt 5.2") != -1 || browser.indexOf("windows xp") != -1)205//"Windows xp"
    else if(browser.indexOf("windows nt 6.1") != -1)206//"Windows 7"
    else if(browser.indexOf("windows nt 6.2") != -1 || browser.indexOf("windows nt 6.3") != -1)207//"Windows 8"
    else if(browser.indexOf("windows nt 10") != -1 || browser.indexOf("windows nt 6.4") != -1)208//"Windows 10"
    else if(browser.indexOf("mac os x") != -1)209//"Mac OS"
    else if(browser.indexOf("linux") != -1)210//"Linux"
    else if(browser.indexOf("unix") != -1)211//"Unix"
    else if(browser.indexOf("freebsd") != -1)212//"FreeBSD"
    else if(browser.indexOf("bing") != -1)213//"必应爬虫"
    else if(browser.indexOf("baiduspider") != -1)214//"百度爬虫"
    else if(browser.indexOf("googlebot") != -1)215//"Googole爬虫"
    else if(browser.indexOf("msnbot-media") != -1)216//"微软爬虫"
    else if(browser.indexOf("yandex") != -1)217//"yandex爬虫"
    else if(browser.indexOf("yunguance") != -1)218//"百度云观测爬虫"
    else 299 //其他PC端系统
  }
}
